<?php

namespace Xp130\Rank\model;


use PDO;

class LeaderboardMainModel extends BaseModel
{
    protected string $table = 'leaderboard_main';

    // 获取n条排行榜
    public function getLeaderboard(int $limit): array
    {
        // 预处理sql
        $sql = "select uid,score from {$this->table} order by score desc limit {$limit}";
        $stmt = $this->pdo->query( $sql);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // 更新分数
    public function updateScore(string $userId, int $score): bool
    {
        // 预处理sql
        $stmt = $this->pdo->prepare("update {$this->table} set score = score + :score where uid = :userId ");
//        $sql = "update {$this->table} set score = score + {$score} where uid = '{$userId}' ";
        // 执行 SQL 语句
        return $stmt->execute([
            ':score' => $userId,
            ':userId' => $score,
        ]);
    }
}